package cc.idoone.spring_boot_hello.config;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

/**
 * 数据转换配置
 *
 * @author hemj
 * @since 2017/10/21
 */
@Configuration
public class ConvertConfig {

	private static String[] datePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd"};

	/**
	 * 时间转换配置
	 */
	@Bean
	public Converter<String, Date> dateInputConvert() {
		return new Converter<String, Date>() {
			@Override
			public Date convert(String source) {
				try {
					return DateUtils.parseDate(source, datePatterns);
				} catch (ParseException e) {
					e.printStackTrace();
					return null;
				}
			}
		};
	}

}
